using System;
using System.Collections.Generic;
using System.Text;
using FutureOpen.Core.func.analyzer;
using Lucene.Net.Search;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Documents;
using Lucene.Net.Analysis;
using Lucene.Net.Highlight;
using System.IO;
using FutureOpen.Core.func.util;


namespace FutureOpen.Core.func.search
{

    public class HighLightUtil
    {
        private Highlighter light = null;
        private static readonly int maxNumFragmentsRequired = 3;
        private static readonly string omitSplitter = "....";
        private static ShootAnalyzer analyzer = new ShootAnalyzer();


        public HighLightUtil(Query query)
        {
            light = new Highlighter(new SimpleHTMLFormatter("<font color=\"red\">", "</font>"), new QueryScorer(query));
            light.SetTextFragmenter(new SimpleFragmenter(65));
        }
        public string GetSnippet(TermPositionVector tpv, String text)
        {
            TokenStream tokenStream = null;
            try
            {

                tokenStream = TokenSources.GetTokenStream(tpv);
            }
            catch (Exception)
            {
                tokenStream = analyzer.TokenStream("", new StringReader(text));
            }

            return light.GetBestFragments(tokenStream, text, maxNumFragmentsRequired, omitSplitter);
        }
    }

    public class TagUtil
    {
        Highlighter light = null;

        public TagUtil(Query query)
        {
            light = new Highlighter(new SimpleHTMLFormatter("<b>", "</b>"), new QueryScorer(query));           
        }
        public string GetContent(TermPositionVector tpv, String text)
        {
            TokenStream tokenStream = null;

            string str = null;
            try
            {
                tokenStream = TokenSources.GetTokenStream(tpv);
                str = light.GetBestFragment(tokenStream, text);
            }
            catch (Exception)
            {
                str = StringUtil.ShortenStr(text);
            }
            return str;

        }
    }
}
